<?php

	/**
     * Acl
	 *
	 * @package      Plugins
	 * @subpackage   Acl
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
	 */
    class Plugin_Acl
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Plugin configuration values
         *
         * @var Zend_Config
         */
        protected $_config = null;

		/**
		 * Zend_Registry object
		 *
		 * @var Zend_Registry
		 */
		protected $_registry = null;

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Returns dependencies list.
         *
         * @return      array
         */
        public function getDependencies()
        {
            return array();
        }

        /**
         * Contructor
         *
         * throws       Core_Exception
		 */
        public function __construct()
        {
            $this->_registry = Zend_Registry::getInstance();

            $configuration   = Core_Plugin_Config::getInstance('config.ini.php', $this->_registry['section']);
            $this->_config   = $configuration->{'acl'};

            if (!empty($this->_config->requires)) {
                $required = explode(',', $this->_config->requires);

                foreach ($required as $module) {
                    if (!array_key_exists($module, $this->_registry['modules'])) {
                        throw new Core_Exception('Missing plugin dependency: ' . $module);
                    }
                }
            }
        }

		/**
		 * Destructor
		 */
		public function __destruct()
		{
			unset($this);
		}

        /**
         * @param       array       $params : plugin parameters for the current action
         * @return      Core_Selector_Action|null    if action should change
         */
        public function beforeAction($params)
        {
            $aclok       = true;
            $selector    = null;
            $coordinator = Core_Controller_Coordinator::getInstance();
            $aclRequired = isset($params['acl.required']) ? ($params['acl.required'] == true) : false;

            if ($aclRequired) {
                $aclok         = false;
                $authNamespace = Core_Authentification::getNamespace();

                if (Core_Authentification::isConnected()) {
                    if (isset($authNamespace->authorized)) {
                        $module = $coordinator->getModule();

                        foreach ($authNamespace->authorized as $package) {
                            foreach ($package['modules'] as $slug => $mod) {
                                if (strtolower($slug) == strtolower($module)) {
                                    $aclok = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (!$aclok) {
                $request = $coordinator->getRequest();

                if ($this->_config->error->insufficient == 1) {// || !$request->isAllowedResponse('redirect')) {
                    throw new Core_Exception($this->_config->error->message);
                } else {
                    $selector = new Core_Selector_GenericAction($this->_config->error->action);
                }
            }

            return $selector;
        }

        public function beforeOutput() { }
        public function afterProcess() { }
    }
